<?php
namespace Home\Event;
//set_time_limit(100);
class WeixinWebEvent {
    public $app_id;
    public $secret;
    public $openid;
    public function __construct(){
        $this->app_id = C('APPID');
        $this->secret = C('SECRET');
    }

    /**
     * 获取token
     */
    public function accessToken($code)
    {
        $getUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->secret}&code={$code}&grant_type=authorization_code";
        $data = file_get_contents($getUrl);
        $array = json_decode($data, true);
        //dump($array);
        //todo 注意。这需要查询用户信息，根据用户信息id做缓存
        //$stat = M('b2c_temp_user')->where(array('openid'=>$array['openid']))->find();
        $this->openid = $array['openid'];
        S($array['openid'].'page_access_token',null);
        S($array['openid'].'page_access_token', $array['access_token'], 7000);
        //有效期30天
        S($array['openid'].'refresh_token',null);
        S($array['openid'].'refresh_token', $array['refresh_token'], 2592000);
        S($array['openid'].'openid',null);
        S($array['openid'].'openid', $array['openid'], 7000);
        //$data = $this->weiXinWebUserName($array['access_token']);
        $obj = A('Weixin','Event');
        $data = $obj->weiXinUserName($array['openid']);
        return $data;
    }
        /**
     * 验证网页token是否有效如果失效者重新请求
     * @param $access_token
     * @return bool
     * access_token 成功
     * 1002  refresh_token 过期
     * 1003 请求刷新access_token失败
     *
     */
    public function auth($access_token){
        $getUrl = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid=OPENID".$this->app_id;
        $data = file_get_contents($getUrl);
        $array = json_decode($data,true);
        if($array['errcode'] == 0){
            return 1001;
        }
        $refresh_token = S('refresh_token');
        //$refresh_token 过期
        if(!$refresh_token){
            return 1002;
        }
        $getUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->app_id}&grant_type={$refresh_token}&refresh_token=".$refresh_token;
        $data = file_get_contents($getUrl);
        $array = json_decode($data,true);
        if(isset($array['errcode'])){
            return 1003;
        }
        S($this->openid.'page_access_token',$array['access_token'],7000);
        //有效期30天
        if($refresh_token != $array['refresh_token']){
            S($this->openid.'refresh_token',$array['refresh_token'],2592000);
        }
        return $array['refresh_token'];
    }
    /**
     * 获取微信用户信息
     * @param $access_token
     * @return bool|mixed
     */
    static $error = 0;
    public function weiXinWebUserName($access_token){
        //$access_token =accessToken();
        //dump($access_token);
        $getUrl = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$this->app_id}&lang=zh_CN";
        $data = get_oneself($getUrl);
        /*if(isset($data['errcode'])){
            $data_token = $this->auth($access_token);
            if(self::$error < 2){
                $this->weiXinWebUserName($data_token);
            }
        }*/
        //dump($data);die;
        return $data;
    }

   

}